package com.yeren.thread.threadPool.fixed;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Java线程：线程池
 * 
 * @author liubiao
 *
 */
public class TestThreadPool {

	public static void main(String[] args) {
		// 创建一个可重用固定线程数的线程池
		ExecutorService pool = Executors.newFixedThreadPool(3);//固定大小的线程池
		
		//创建一个使用单个 worker 线程的 Executor，以无界队列方式来运行该线程。 
//        ExecutorService pool = Executors.newSingleThreadExecutor();//单任务线程池
		
		 //创建一个可根据需要创建新线程的线程池，但是在以前构造的线程可用时将重用它们。 
//        ExecutorService pool = Executors.newCachedThreadPool();//可变尺寸的线程池
		
		// 创建实现了Runnable接口对象，Thread对象当然也实现了Runnable接口
		Thread t1 = new MyThreadPool();
		Thread t2 = new MyThreadPool();
		Thread t3 = new MyThreadPool();
		Thread t4 = new MyThreadPool();
		Thread t5 = new MyThreadPool();
		// 将线程放入池中进行执行
		pool.execute(t1);
		pool.execute(t2);
		pool.execute(t3);
		pool.execute(t4);
		pool.execute(t5);
		// 关闭线程池
		pool.shutdown();
	}
}

class MyThreadPool extends Thread {
	public void run() {
		System.out.println(Thread.currentThread().getName() + "正在执行。。。");
	}
}